package com.founder.gateway.component.transport.netty.extension.support;

import java.io.IOException;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.founder.gateway.component.transport.netty.exception.NettyException;
import com.founder.gateway.component.transport.netty.filter.NettyFilterChain;
import com.founder.gateway.component.transport.netty.http.NettyHttpRequest;
import com.founder.gateway.component.transport.netty.http.NettyHttpResponse;
import com.founder.gateway.system.consts.ApiSetuErrorCode;

public class SentinelNettyHttpFilter extends AbstractNettyHttpFilter {
    @Override
    public void doFilter(NettyHttpRequest request, NettyHttpResponse response, NettyFilterChain chain)
            throws IOException, NettyException {
        Entry entry = null;
        try {
            entry = SphU.entry(chain.getApiSchemeEntity().getId());
            chain.doFilter(request, response);
        } catch (BlockException e1) {
            throw new NettyException(ApiSetuErrorCode.API_SENTINEL_BLOCK);
        } finally {
            if (entry != null) {
                entry.exit();
            }
        }
    }
}
